﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using ScrumTable.Common;
using ScrumTable.Common.Security;

#endregion

namespace ScrumTable.DL.Data.TFS.Services.TeamFoundation
{
    /// <summary>
    /// Wraps the internal TeamFoundationServer instance.
    /// </summary>
    public partial class TfsServerWrapper
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const string Tfs11MemberIdAPIUri = "{0}/_api/_common/IdentityImage?id={1}";

        private ICommonStructureService4 _structureService;
        private TfsTeamService _tfsTeamService;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the TFS structure service which contains additional information about areas/iterations.
        /// </summary>
        public ICommonStructureService4 StructureService
        {
            get { return _structureService; }
        }

        /// <summary>
        /// The TfsTeamService can be used to create, update or query teams, as well as retrieve the default team for the project.
        /// </summary>
        public TfsTeamService TfsTeamService
        {
            get { return _tfsTeamService; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Gets the API uri for the member image.
        /// </summary>
        /// <param name="memberId">Specifies the id of the member to retrieve its image from.</param>
        public Uri GetTeamIdentityImageUri(string memberId)
        {
            Uri identityImageUri;

            // Source: http://sinv0008.hsr.ch:8080/tfs/TestProjects/_api/_common/IdentityImage?id=da334f24-fe15-4076-8cb0-17d37e4bdf4b&6a0b4139-408b-4490-8731-2c7d28ac8289
            if (!string.IsNullOrEmpty(memberId)
                && Uri.TryCreate(string.Format(
                        Tfs11MemberIdAPIUri,
                        _teamFoundationServer.Uri,
                        memberId), UriKind.Absolute,
                out identityImageUri))
            {
                return identityImageUri;
            }
            return null;
        }

        /// <summary>
        /// Virual constructor call from partial class.
        /// </summary>
        partial void Initialize()
        {
            _structureService = GetService<ICommonStructureService4>();
            _tfsTeamService = GetService<TfsTeamService>();
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}